package br.com.peperoncino.controller.cardapio;

import java.math.BigDecimal;
import java.net.URL;
import java.util.Arrays;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import br.com.peperoncino.entity.ItemCardapio;
import br.com.peperoncino.entity.TipoItem;
import br.com.peperoncino.model.ItemCardapioDAO;
import br.com.peperoncino.utils.StringHelper;

public class ManterItemCardapioController implements Initializable {
	
	public void carregaValoresItemCardapio(ItemCardapio itemCardapio) {
		codigoInput.setText(itemCardapio.getId()!= null ? itemCardapio.getId().toString() : null);
		nomeInput.setText(itemCardapio.getNome());
		descricaoInput.setText(itemCardapio.getDescricao());
		valorInput.setText(itemCardapio.getValor() != null ? itemCardapio.getValor().toString() : null);
		tipoItemInput.setValue(itemCardapio.getTipoItem());
	}
	
	@FXML
	TextField codigoInput;
	
	@FXML
	TextField nomeInput;
	
	@FXML
	TextArea descricaoInput;
	
	@FXML
	TextField valorInput;
	
	@FXML
	ComboBox<TipoItem> tipoItemInput = new ComboBox<TipoItem>();
	
	@FXML
	Button salvar;
	
	@FXML
	Button novo;

	@FXML
	Button excluir;

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
			
		/*popula o combo*/
		tipoItemInput.setItems(FXCollections.observableArrayList(Arrays.asList(TipoItem.values())));
	}
	
	public void aoSalvarItem(ActionEvent event){
		
		//Form Validation
//		if(name && date && price){
		
			Integer codigo = !StringHelper.ehBrancoOuNulo(codigoInput.getText()) ? Integer.parseInt(codigoInput.getText()) : null;  
			
			ItemCardapio itemCardapio = new ItemCardapio(codigo, nomeInput.getText(), descricaoInput.getText(), 
					 new BigDecimal(valorInput.getText()), tipoItemInput.getValue());
			
			new ItemCardapioDAO().salvarItemCardapio(itemCardapio);
			
			codigoInput.setText(itemCardapio.getId() != null ? itemCardapio.getId().toString() : null);
//		}
		
		
	}
	
	public void aoIncluir(ActionEvent event){
		limparForm();
	}

	public void aoExcluir(ActionEvent event){
		
		Integer codigo = !StringHelper.ehBrancoOuNulo(codigoInput.getText()) ? Integer.parseInt(codigoInput.getText()) : null;  
		
		ItemCardapio itemCardapio = new ItemCardapio(codigo, nomeInput.getText(), descricaoInput.getText(), 
				 new BigDecimal(valorInput.getText()), tipoItemInput.getValue());
		
		new ItemCardapioDAO().deleteItemCardapio(itemCardapio);
		
		limparForm();
	}
	
	private void limparForm(){
		
		codigoInput.clear();
		nomeInput.clear();
		descricaoInput.clear();
		valorInput.clear();
		tipoItemInput.setValue(null);
		
	}


}
